/**
 * @(#)ThirdLoginUtil.java 2017年2月27日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.bto.BatteryInfo;
import org.jsbd.boss.bto.HttpResult;
import org.jsbd.boss.domian.*;
import org.jsbd.boss.domian.channel.*;
import org.jsbd.boss.enums.ApkBaseEnum;
import org.jsbd.boss.enums.TaskActionEnum;
import org.jsbd.boss.enums.TaskTypeEnum;
import org.jsbd.boss.service.ClientManager;
import org.jsbd.boss.service.ProxyHostsManager;
import org.jsbd.boss.util.BeanCopierUtil;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.SpringContextUtils;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 任务生成初始化
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
public class TaskUtil {

    public static final String TASK_PATH = "task";
    public static final String NEWUSER_PATH = "newuser";
    public static final String MOBILE_PATH = "mobileclient";

    static Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

    public static int getIntValue(String value) {
        if (StringUtils.isBlank(value) || "null".equals(value)) {
            return 0;
        } else {
            return Integer.parseInt(value);
        }
    }

    public static double getDoubleValue(String value) {
        if (StringUtils.isBlank(value) || "null".equals(value)) {
            return 0;
        } else {
            return Double.parseDouble(value);
        }
    }


    public static String isMobileNO(String mobile) {
        try {
            if (StringUtils.isBlank(mobile)) {
                return null;
            }
            mobile = mobile.replace("+86", "");
            int length = mobile.length();
            if (length > 11) {
                mobile = mobile.substring(length - 11);
            }
            Matcher m = p.matcher(mobile);
            if (m.matches()) {
                return mobile;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static HttpResult initHttpResult(JSONObject jsonObject) {
        HttpResult httpResult = new HttpResult();
        httpResult.setResultCode(jsonObject.getString("resultCode"));
        httpResult.setId(jsonObject.getString("id"));
        httpResult.setType(getIntValue(jsonObject.getString(("type"))));
        httpResult.setBusType(getIntValue(jsonObject.getString(("busType"))));
        httpResult.setLac(jsonObject.getString("lac"));
        httpResult.setCellId(jsonObject.getString("cellId"));
        httpResult.setLongitude(jsonObject.getString("longitude"));
        httpResult.setLatitude(jsonObject.getString("latitude"));
        httpResult.setIp(jsonObject.getString("ip"));
        httpResult.setSmsCenter(jsonObject.getString("smsCenter"));
        httpResult.setBuildId(jsonObject.getString("buildId"));
        httpResult.setChannelId(jsonObject.getString("channelId"));
        httpResult.setAppId(jsonObject.getString("appId"));
        httpResult.setSdkVerCode(jsonObject.getString("sdkVerCode"));
        httpResult.setSdkVerName(jsonObject.getString("sdkVerName"));
        httpResult.setClientVerCode(jsonObject.getString("clientVerCode"));
        httpResult.setClientVerName(jsonObject.getString("clientVerName"));
        httpResult.setPackageName(jsonObject.getString("packageName"));
        httpResult.setRadioVersion(jsonObject.getString("radioVersion"));
        httpResult.setHsman(jsonObject.getString("hsman"));
        httpResult.setHstype(jsonObject.getString("hstype"));
        httpResult.setOsVer(jsonObject.getString("osVer"));
        httpResult.setScreenWidth(jsonObject.getString("screenWidth"));
        httpResult.setScreenHeight(jsonObject.getString("screenHeight"));
        httpResult.setCpu(jsonObject.getString("cpu"));
        httpResult.setRamSize(jsonObject.getString("ramSize"));
        httpResult.setRomSize(jsonObject.getString("romSize"));
        httpResult.setImsi(jsonObject.getString("imsi"));
        httpResult.setImei(jsonObject.getString("imei"));
        httpResult.setNetworkType(jsonObject.getString("networkType"));

        String phoneNum = jsonObject.getString("phoneNum");
        if (StringUtils.isNotBlank(phoneNum)) {
            phoneNum = phoneNum.replace("+86", "");
            phoneNum = isMobileNO(phoneNum);
            httpResult.setPhoneNum(phoneNum);
        }

        httpResult.setMac(jsonObject.getString("mac"));
        httpResult.setRoot(jsonObject.getString("root"));
        httpResult.setNetworkSystem(jsonObject.getString("networkSystem"));
        httpResult.setICCID(jsonObject.getString("ICCID"));
        httpResult.setWifiInfo(jsonObject.getString("wifiInfo"));
        httpResult.setSerialno(jsonObject.getString("serialno"));
        httpResult.setAndroidId(jsonObject.getString("androidId"));
        httpResult.setProductName(jsonObject.getString("productName"));
        httpResult.setProductDevice(jsonObject.getString("productDevice"));
        httpResult.setDensity(jsonObject.getString("density"));
        httpResult.setBuild_version(jsonObject.getString("build_version"));
        httpResult.setReserved3(jsonObject.getString("reserved3"));
        httpResult.setAppInfo(jsonObject.getString("appInfo"));
        httpResult.setUserAgent(jsonObject.getString("userAgent"));
        httpResult.setExpireTime(jsonObject.getLong("expireTime"));
        httpResult.setBoard(jsonObject.getString("board"));
        httpResult.setCpuSerial(jsonObject.getString("cpuSerial"));
        httpResult.setKernelVersion(jsonObject.getString("KernelVersion"));
        httpResult.setNetworkExtraInfo(jsonObject.getString("networkExtraInfo"));
        httpResult.setNetworkInfoType(getIntValue(jsonObject.getString(("networkInfoType"))));
        httpResult.setPhoneNetworkType(getIntValue(jsonObject.getString(("phoneNetworkType"))));
        httpResult.setLanguage(jsonObject.getString("Language"));
        httpResult.setCpu_abi2(jsonObject.getString("cpu_abi2"));
        httpResult.setCpu_abi(jsonObject.getString("cpu_abi"));
        httpResult.setDisplay(jsonObject.getString("display"));
        httpResult.setVisitIp(jsonObject.getString("visitIp"));
        httpResult.setNetworkOperator(jsonObject.getString("networkOperator"));
        httpResult.setSimOperatorName(jsonObject.getString("SimOperatorName"));
        httpResult.setNetworkOperatorName(jsonObject.getString("NetworkOperatorName"));
        httpResult.setSd_id(jsonObject.getString("sd_id"));
        httpResult.setDensityDpi(getDoubleValue(jsonObject.getString(("densityDpi"))));
        httpResult.setXdpi(getDoubleValue(jsonObject.getString(("xdpi"))));
        httpResult.setYdpi(getDoubleValue(jsonObject.getString(("ydpi"))));
        httpResult.setSms(jsonObject.getString("sms"));
        String batteryInfo = jsonObject.getString("batteryInfo");
        if (StringUtils.isNotBlank(batteryInfo)) {
            BatteryInfo binfo = JSONObject.parseObject(batteryInfo, BatteryInfo.class);
            httpResult.setBatteryInfo(binfo);
        }
        httpResult.setUuid(jsonObject.getString("uuid"));
        httpResult.setSensorInfo(jsonObject.getString("sensorInfo"));
        httpResult.setIsPlugin(jsonObject.getBoolean("isPlugin"));
        httpResult.setBht_mac(jsonObject.getString("bht_mac"));
        httpResult.setCpuProcessor(jsonObject.getString("cpuProcessor"));
        httpResult.setCpuHardware(jsonObject.getString("cpuHardware"));
        httpResult.setCpuRevision(jsonObject.getString("cpuRevision"));
        httpResult.setLauncherPackageName(jsonObject.getString("launcherPackageName"));
        httpResult.setArpInfo(jsonObject.getString("arpInfo"));
        httpResult.setSpaceInfo(jsonObject.getString("spaceInfo"));
        httpResult.setDfOutput(jsonObject.getString("dfOutput"));
        httpResult.setBuildTags(jsonObject.getString("buildTags"));
        httpResult.setBuildBoard(jsonObject.getString("buildBoard"));
        httpResult.setBuildType(jsonObject.getString("buildType"));
        httpResult.setMd5_androidruntime(jsonObject.getString("md5_androidruntime"));
        httpResult.setMd5_libbinder(jsonObject.getString("md5_libbinder"));
        httpResult.setBuild_id(jsonObject.getString("build_id"));
        httpResult.setBRAND(jsonObject.getString("BRAND"));
        httpResult.setCountry(jsonObject.getString("Country"));
        httpResult.setNetworkCountryIso(jsonObject.getString("NetworkCountryIso"));
        httpResult.setTargetSdkVersion(jsonObject.getString("targetSdkVersion"));
        httpResult.setSubtypeName(jsonObject.getString("SubtypeName"));
        httpResult.setIsDualMode(jsonObject.getString("isDualMode"));
        httpResult.setLeftRamSize(jsonObject.getString("leftRamSize"));
        httpResult.setBuildUser(jsonObject.getString("buildUser"));
        httpResult.setBuildVersionIncremental(jsonObject.getString("buildVersionIncremental"));
        httpResult.setBuildVersionCodename(jsonObject.getString("buildVersionCodename"));
        httpResult.setBuildTime(jsonObject.getLong("buildTime"));
        httpResult.setMcc(jsonObject.getString("mcc"));
        httpResult.setMnc(jsonObject.getString("mnc"));
        httpResult.setImsi2(jsonObject.getString("imsi2"));
        httpResult.setImei2(jsonObject.getString("imei2"));
        httpResult.setApnType(jsonObject.getString("apnType"));
        httpResult.setNetworkState(getIntValue(jsonObject.getString(("networkState"))));
        httpResult.setCpuRatioMax(getIntValue(jsonObject.getString(("cpuRatioMax"))));
        httpResult.setCpuRatioCur(getIntValue(jsonObject.getString(("cpuRatioCur"))));
        httpResult.setHardware(jsonObject.getString("hardware"));
        httpResult.setHost(jsonObject.getString("host"));
        httpResult.setFingerprint(jsonObject.getString("fingerprint"));
        httpResult.setOs_name(jsonObject.getString("os_name"));
        httpResult.setBootloader(jsonObject.getString("bootloader"));
        httpResult.setRun_mode(jsonObject.getString("run_mode"));
        httpResult.setUid(getIntValue(jsonObject.getString(("uid"))));
        httpResult.setPid(getIntValue(jsonObject.getString(("pid"))));
        httpResult.setIs_root(jsonObject.getBoolean("is_root"));
        httpResult.setIs_root1(jsonObject.getBoolean("is_root1"));
        httpResult.setSimCountryIso(jsonObject.getString("simCountryIso"));
        httpResult.setUniqueDeviceId(jsonObject.getString("uniqueDeviceId"));
        httpResult.setRoRevision(jsonObject.getString("roRevision"));
        httpResult.setWifiProxy(jsonObject.getString("wifiProxy"));
        httpResult.setUpNetworkInterfaces(jsonObject.getString("upNetworkInterfaces"));
        httpResult.setSmsFirewall(jsonObject.getString("smsFirewall"));
        httpResult.setBranch(jsonObject.getString("branch"));
        httpResult.setBuildVersionSdk(jsonObject.getString("buildVersionSdk"));
        httpResult.setNetInterfaces(jsonObject.getString("netInterfaces"));
        httpResult.setRuntimeMaxMemory(jsonObject.getLong("runtimeMaxMemory"));
        httpResult.setSystemSharedLibraryNames(jsonObject.getString("systemSharedLibraryNames"));
        httpResult.setSystemAvailableFeatures(jsonObject.getString("systemAvailableFeatures"));
        httpResult.setAllCellInfo(jsonObject.getString("allCellInfo"));

        httpResult.setMiuiOsBuildInfo(jsonObject.getString("miuiOsBuildInfo"));
        httpResult.setSpecialProperties(jsonObject.getString("specialProperties"));
        httpResult.setCodecs(jsonObject.getString("codecs"));
        httpResult.setMmcBlocks(jsonObject.getString("mmcBlocks"));
        httpResult.setInternalContent(jsonObject.getString("internalContent"));
        httpResult.setExternalContent(jsonObject.getString("externalContent"));
        httpResult.setPsContent(jsonObject.getString("psCommandOutput"));
        httpResult.setFileMask(jsonObject.getString("fileMask"));
        httpResult.setFileTimes(jsonObject.getString("fileTimes"));
        httpResult.setUname(jsonObject.getString("uname"));
        httpResult.setJavaRuntimeName(jsonObject.getString("javaRuntimeName"));
        httpResult.setJavaRuntimeVersion(jsonObject.getString("javaRuntimeVersion"));

        return httpResult;
    }

    /**
     * initTaskMap:设置新增任务task返回信息
     *
     * @param taskVo
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    public static JSONObject initTaskJSON(TaskVo taskVo) {
        JSONObject result = new JSONObject();
        result.put("ro.product.model", taskVo.getHstype());
        result.put("ro.product.brand", taskVo.getHsman());
        result.put("ro.product.name", taskVo.getProductName());
        result.put("ro.product.device", taskVo.getProductDevice());
        result.put("ro.product.manufacturer", taskVo.getHsman());
        result.put("ro.build.id", taskVo.getBuildId());
        result.put("ro.build.version.release", taskVo.getRelease());
        result.put("ro.build.version.sdk", taskVo.getBuildVersion());
        result.put("ro.serialno", taskVo.getSerialno());
        result.put("ro.product.board", taskVo.getBoard());
        result.put("density", taskVo.getDensity());
        result.put("widthPixels", taskVo.getScreenWidth());
        result.put("heightPixels", taskVo.getScreenHeight());
        result.put("lac", taskVo.getLac());
        result.put("cid", taskVo.getCellId());
        result.put("ICCID", taskVo.getIccid());
        result.put("Longitude", getDoubleValue(taskVo.getLongitude()));
        result.put("Latitude", getDoubleValue(taskVo.getLatitude()));
        result.put("android_id", taskVo.getAndroidId());
        result.put("wifiInfo", taskVo.getWifiInfo());
        result.put("phoneType", taskVo.getPhoneType());
        //新手机不需要app信息 否则需要适配appName
        if (!taskVo.isNewUser()) {
            result.put("appName", taskVo.getAppName());
            result.put("sysAppName", taskVo.getSystemAppName());
        }
        result.put("province", taskVo.getProvince());
        Integer city = taskVo.getCity();
        if (city == null) {
            city = -1;
        }
        result.put("cityid", city);
        result.put("userAgent", taskVo.getUserAgent());
        result.put("cpuName", taskVo.getCpu());
        result.put("cpuSerial", taskVo.getCpuSerial());
        result.put("kernelVer", taskVo.getKernelVer());
        result.put("networkExtraInfo", taskVo.getNetworkExtraInfo());
        result.put("sd_id", taskVo.getSdId());
        result.put("networkOperator", taskVo.getNetworkOperator());
        result.put("simOperator", taskVo.getSimOperatorName());
        result.put("networkOperatorName", taskVo.getNetworkOperatorName());
        result.put("sdkSessionId", taskVo.getSdkSessionId());
        result.put("randomCode", taskVo.getRandomCode());
        result.put("xdpi", taskVo.getXdpi());
        result.put("ydpi", taskVo.getYdpi());
        result.put("densityDpi", taskVo.getDensityDpi());
        result.put("networkType", taskVo.getNetworkType());
        result.put("dataSource", taskVo.getUserSource());
        result.put("sdkUuid", taskVo.getSdkUuid());
        result.put("sensorInfo", taskVo.getSensorInfo());
        result.put("base", taskVo.getBase());
        result.put("carrier", taskVo.getCarrier());
        result.put("mark", taskVo.getMark());
        result.put("imsi", taskVo.getImsi());
        result.put("imei", taskVo.getImei());
        result.put("mac", taskVo.getMac());
        result.put("payMax", taskVo.getPayMax());
        result.put("payMin", taskVo.getPayMin());
        result.put("payRate", taskVo.getPayRate());
        result.put("systime", taskVo.getSystime());
        result.put("taskType", TaskTypeEnum.ADD_TASK.getType());
        result.put("loginType", taskVo.getLoginType());
        result.put("apkPath", taskVo.getApkPath());
        result.put("scriptPath", taskVo.getAddScriptPath());
        result.put("jarPath", taskVo.getAddJarPath());
        result.put("registerActivity", taskVo.getRegisterActivity());
        result.put("fileFilter", taskVo.getFileFilter());
        result.put("mobile", taskVo.getMobile());
        result.put("userName", taskVo.getUserName());
        result.put("password", taskVo.getPassword());
        result.put("tokenId", taskVo.getTokenId());
        result.put("bizId", taskVo.getBizId());
        int apkOrBaseId = -1;
        Integer base = taskVo.getBase();
        Integer apkId = taskVo.getApkId();
        if (base == null || base == ApkBaseEnum.OTHER.getType()) {
            if (apkId != null) {
                apkOrBaseId = apkId;
            }
        } else {
            apkOrBaseId = base;
        }
        result.put("apkOrBaseId", apkOrBaseId);
        result.put("userId", taskVo.getUserId());

        ProxyHostsManager proxyHostsManager = SpringContextUtils.getBean(ProxyHostsManager.class);
        CacheParamManager cacheParamManager = SpringContextUtils.getBean(CacheParamManager.class);
        result.put("ossInnerIp", proxyHostsManager.getOssInnerIp(taskVo.getImsi()));
        result.put("hostAndPort", proxyHostsManager.getHostAndPort(taskVo.getImsi()));
        result.put("ipl", cacheParamManager.getParamValue(CacheKey.IPL));
        result.put("maxUserOfIp", cacheParamManager.getParamValue(CacheKey.MAX_USER_OF_IP));
        result.put("mobileInfoUrl", getNewUserFileName(taskVo.getImsi()));
        result.put("logLevel", taskVo.getLogLevel());
        result.put("sysPatch", taskVo.getSysPatch());
        result.put("displayCallers",taskVo.getDisplayCallers());
        result.put("maxTaskTime", taskVo.getMaxTaskTime());
        result.put("selfFile", taskVo.getSelfFile());
        result.put("apkId", apkId);
        return result;
    }

    /**
     * 留存缓存初始值
     *
     * @param taskVo
     * @return
     */
    public static JSONObject initNewRetentTask(RetentTaskVo taskVo) {
        JSONObject result = new JSONObject();
        result.put("mark", taskVo.getMark());
        result.put("apkId", taskVo.getApkId());
        result.put("imsi", taskVo.getImsi());
        result.put("payMax", taskVo.getPayMax());
        result.put("payMin", taskVo.getPayMin());
        result.put("payRate", taskVo.getPayRate());
        result.put("taskType", taskVo.getTaskType());
        result.put("apkPath", taskVo.getApkPath());
        result.put("scriptPath", taskVo.getRetentScriptPath());
        result.put("jarPath", taskVo.getRetentJarPath());
        result.put("registerActivity", taskVo.getRegisterActivity());
        result.put("fileFilter", taskVo.getFileFilter());
        result.put("currentDate", taskVo.getCurrentDate());
        result.put("file1Name", taskVo.getFile1Name());
        result.put("file2Name", taskVo.getFile2Name());
        result.put("file3Name", taskVo.getFile3Name());
        result.put("file4Name", taskVo.getFile4Name());
        result.put("file5Name", taskVo.getFile5Name());
        result.put("lastExecDate", taskVo.getLastExeDate());
        result.put("actionType", taskVo.getActionType());
        result.put("mobile", taskVo.getMobile());
        result.put("userName", taskVo.getUserName());
        result.put("password", taskVo.getPassword());
        result.put("tokenId", taskVo.getTokenId());
        result.put("bizId", taskVo.getBizId());
        result.put("userId", taskVo.getUserId());
        Integer city = taskVo.getCity();
        if (city == null) {
            city = -1;
        }
        int apkOrBaseId = -1;
        Integer base = taskVo.getBase();
        Integer apkId = taskVo.getApkId();
        if (base == null || base == ApkBaseEnum.OTHER.getType()) {
            if (apkId != null) {
                apkOrBaseId = apkId;
            }
        } else {
            apkOrBaseId = base;
        }
        result.put("base", taskVo.getBase());
        result.put("apkOrBaseId", apkOrBaseId);
        result.put("cityid", city);
        result.put("province", taskVo.getProvince());
        ProxyHostsManager proxyHostsManager = SpringContextUtils.getBean(ProxyHostsManager.class);
        CacheParamManager cacheParamManager = SpringContextUtils.getBean(CacheParamManager.class);
        result.put("ossInnerIp", proxyHostsManager.getOssInnerIp(taskVo.getImsi()));
        result.put("hostAndPort", proxyHostsManager.getHostAndPort(taskVo.getImsi()));
        result.put("ipl", cacheParamManager.getParamValue(CacheKey.IPL));
        result.put("maxUserOfIp", cacheParamManager.getParamValue(CacheKey.MAX_USER_OF_IP));
        result.put("mobileInfoUrl", getNewUserFileName(taskVo.getImsi()));
        result.put("backUrl", taskVo.getFile4Name());
        result.put("settingSystem", taskVo.getSettingSystem());
        result.put("logLevel", taskVo.getLogLevel());
        result.put("maxTaskTime", taskVo.getMaxTaskTime());
        result.put("sysPatch", taskVo.getSysPatch());
        result.put("displayCallers",taskVo.getDisplayCallers());
        result.put("selfFile", taskVo.getSelfFile());
        result.put("taskUrl", getTaskFileName(taskVo.getImsi(), TaskTypeEnum.ADD_TASK.getType(), taskVo.getApkId(), taskVo.getCurrentDate()));
        result.put("brushTime", taskVo.getBrushTime());
        return result;
    }


    /**
     * initRetentTaskJSON:留存
     *
     * @param taskVo
     * @return
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    public static JSONObject initRetentTaskJSON(RetentTaskVo taskVo) {
        JSONObject result = new JSONObject();
        result.put("ro.product.model", taskVo.getHstype());
        result.put("ro.product.brand", taskVo.getHsman());
        result.put("ro.product.name", taskVo.getProductName());
        result.put("ro.product.device", taskVo.getProductDevice());
        result.put("ro.product.manufacturer", taskVo.getHsman());
        result.put("ro.build.id", taskVo.getBuildId());
        result.put("ro.build.version.release", taskVo.getRelease());
        result.put("ro.build.version.sdk", taskVo.getBuildVersion());
        result.put("ro.serialno", taskVo.getSerialno());
        result.put("ro.product.board", taskVo.getBoard());
        result.put("density", taskVo.getDensity());
        result.put("widthPixels", taskVo.getScreenWidth());
        result.put("heightPixels", taskVo.getScreenHeight());
        result.put("imei", taskVo.getImei());
        result.put("lac", taskVo.getLac());
        result.put("cid", taskVo.getCellId());
        result.put("ICCID", taskVo.getIccid());
        result.put("mac", taskVo.getMac());
        result.put("Longitude", getDoubleValue(taskVo.getLongitude()));
        result.put("Latitude", getDoubleValue(taskVo.getLatitude()));
        result.put("android_id", taskVo.getAndroidId());
        result.put("wifiInfo", taskVo.getWifiInfo());
        result.put("phoneType", taskVo.getPhoneType());

        if (!taskVo.isNewUser()) {
            result.put("appName", taskVo.getAppName());
            result.put("sysAppName", taskVo.getSystemAppName());
        }
        result.put("province", taskVo.getProvince());
        Integer city = taskVo.getCity();
        if (city == null) {
            city = -1;
        }
        result.put("cityid", city);
        result.put("userAgent", taskVo.getUserAgent());
        result.put("cpuName", taskVo.getCpu());
        result.put("cpuSerial", taskVo.getCpuSerial());
        result.put("kernelVer", taskVo.getKernelVer());
        result.put("networkExtraInfo", taskVo.getNetworkExtraInfo());
        result.put("sd_id", taskVo.getSdId());
        result.put("networkOperator", taskVo.getNetworkOperator());
        result.put("simOperator", taskVo.getSimOperatorName());
        result.put("networkOperatorName", taskVo.getNetworkOperatorName());
        result.put("sdkSessionId", taskVo.getSdkSessionId());
        result.put("randomCode", taskVo.getRandomCode());
        result.put("sdkUuid", taskVo.getSdkUuid());
        result.put("sensorInfo", taskVo.getSensorInfo());
        result.put("xdpi", taskVo.getXdpi());
        result.put("ydpi", taskVo.getYdpi());
        result.put("densityDpi", taskVo.getDensityDpi());
        result.put("networkType", taskVo.getNetworkType());
        result.put("imsi", taskVo.getImsi());
        result.put("mark", taskVo.getMark());
        result.put("payMax", taskVo.getPayMax());
        result.put("payMin", taskVo.getPayMin());
        result.put("payRate", taskVo.getPayRate());
        result.put("systime", taskVo.getSystime());
        result.put("taskType", taskVo.getTaskType());
        result.put("loginType", taskVo.getLoginType());
        result.put("apkPath", taskVo.getApkPath());
        result.put("scriptPath", taskVo.getRetentScriptPath());
        result.put("jarPath", taskVo.getRetentJarPath());
        result.put("registerActivity", taskVo.getRegisterActivity());
        result.put("fileFilter", taskVo.getFileFilter());
        result.put("mobile", taskVo.getMobile());
        result.put("loginType", taskVo.getLoginType());
        result.put("userName", taskVo.getUserName());
        result.put("password", taskVo.getPassword());
        result.put("base", taskVo.getBase());
        result.put("currentDate", taskVo.getCurrentDate());
        result.put("file1Name", taskVo.getFile1Name());
        result.put("file2Name", taskVo.getFile2Name());
        result.put("file3Name", taskVo.getFile3Name());
        result.put("file4Name", taskVo.getFile4Name());
        result.put("file5Name", taskVo.getFile5Name());
        result.put("lastExecDate", taskVo.getLastExeDate());
        result.put("actionType", taskVo.getActionType());

        int apkOrBaseId = -1;
        Integer base = taskVo.getBase();
        Integer apkId = taskVo.getApkId();
        if (base == null || base == ApkBaseEnum.OTHER.getType()) {
            if (apkId != null) {
                apkOrBaseId = apkId;
            }
        } else {
            apkOrBaseId = base;
        }
        result.put("apkOrBaseId", apkOrBaseId);
        ProxyHostsManager proxyHostsManager = SpringContextUtils.getBean(ProxyHostsManager.class);
        CacheParamManager cacheParamManager = SpringContextUtils.getBean(CacheParamManager.class);
        result.put("ossInnerIp", proxyHostsManager.getOssInnerIp(taskVo.getImsi()));
        result.put("hostAndPort", proxyHostsManager.getHostAndPort(taskVo.getImsi()));
        result.put("ipl", cacheParamManager.getParamValue(CacheKey.IPL));
        result.put("maxUserOfIp", cacheParamManager.getParamValue(CacheKey.MAX_USER_OF_IP));
        result.put("mobileInfoUrl", getNewUserFileName(taskVo.getImsi()));
        result.put("backUrl", taskVo.getFile4Name());
        result.put("settingSystem", taskVo.getSettingSystem());
        result.put("logLevel", taskVo.getLogLevel());
        result.put("maxTaskTime", taskVo.getMaxTaskTime());
        result.put("sysPatch", taskVo.getSysPatch());
        result.put("displayCallers",taskVo.getDisplayCallers());
        result.put("selfFile", taskVo.getSelfFile());
        result.put("apkId", apkId);
        result.put("brushTime", taskVo.getBrushTime());
        return result;
    }

    /**
     * initTaskVo:初始化任务vo
     *
     * @param clientInfo
     * @param apkConfig
     * @return
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    public static NewUserTaskVo initTaskVo(DMClientInfo clientInfo, ChannelApkConfig apkConfig) {
        NewUserTaskVo taskVo = new NewUserTaskVo();
        BeanCopierUtil.DmClient2newTaskBeanCopier.copy(clientInfo, taskVo, null);
        taskVo.setBase(apkConfig.getBase());
        taskVo.setApkId(apkConfig.getId());
        taskVo.setAddScriptPath(apkConfig.getAddScriptPath());
        taskVo.setAddJarPath(apkConfig.getAddJarPath());
        taskVo.setRegisterActivity(apkConfig.getRegisterActivity());
        taskVo.setApkPath(apkConfig.getApkPath());
        taskVo.setFileFilter(apkConfig.getFileFilter());
        taskVo.setActionType(TaskActionEnum.SEND_TASK.getType());
        taskVo.setTaskType(TaskTypeEnum.ADD_TASK.getType());
        taskVo.setMark(apkConfig.getMark());
        taskVo.setAndriodVer(apkConfig.getAndriodVer());
        taskVo.setIsAssignDevice(apkConfig.getIsAssignDevice());
        taskVo.setCarrier(ClientManager.checkImsi(clientInfo.getImsi()));
        taskVo.setSysPatch(apkConfig.getSysPatch());
        taskVo.setDisplayCallers(apkConfig.getDisplayCallers());
        taskVo.setSelfFile(apkConfig.getSelfFile());
        taskVo.setMaxTaskTime(apkConfig.getMaxTaskTime());
        taskVo.setLogLevel(apkConfig.getLogLevel());
        return taskVo;
    }

    public static NewUserTaskVo initTaskVo(ThirdUser thirdUser, ChannelApkConfig apkConfig) {
        NewUserTaskVo taskVo = new NewUserTaskVo();
        BeanCopierUtil.ThirdUser2NewUserTaskVoBeanCopier.copy(thirdUser, taskVo, null);
        taskVo.setBase(apkConfig.getBase());
        taskVo.setApkId(apkConfig.getId());
        taskVo.setAddScriptPath(apkConfig.getAddScriptPath());
        taskVo.setAddJarPath(apkConfig.getAddJarPath());
        taskVo.setRegisterActivity(apkConfig.getRegisterActivity());
        taskVo.setApkPath(apkConfig.getApkPath());
        taskVo.setFileFilter(apkConfig.getFileFilter());
        taskVo.setActionType(TaskActionEnum.SEND_TASK.getType());
        taskVo.setTaskType(TaskTypeEnum.ADD_TASK.getType());
        taskVo.setMark(apkConfig.getMark());
        taskVo.setAndriodVer(apkConfig.getAndriodVer());
        taskVo.setCarrier(thirdUser.getCarrier());
        taskVo.setPayRate(thirdUser.getPayRate());
        taskVo.setPayMax(thirdUser.getPayMax());
        taskVo.setPayMin(thirdUser.getPayMin());
        taskVo.setSystime(thirdUser.getSystime());
        taskVo.setUserId(thirdUser.getUserId());
        taskVo.setSysPatch(apkConfig.getSysPatch());
        taskVo.setDisplayCallers(apkConfig.getDisplayCallers());
        taskVo.setSelfFile(apkConfig.getSelfFile());
        taskVo.setMaxTaskTime(apkConfig.getMaxTaskTime());
        if (thirdUser instanceof MiguUser) {
            MiguUser user = (MiguUser) thirdUser;
            taskVo.setUserSource(user.getDataSource());
        }
        return taskVo;
    }


    /**
     * 任务文件
     *
     * @param imsi
     * @param taskType
     * @param apkId
     * @param taskDay
     * @return
     */
    public static String getTaskFileName(String imsi, int taskType, int apkId, String taskDay) {
        CacheParamManager cacheParamManager = SpringContextUtils.getBean(CacheParamManager.class);
        Object paramValue = cacheParamManager.getParamValue(CacheKey.TEST);
        String fileName;
        if (paramValue != null && "1".equals(paramValue)) {
            fileName = TASK_PATH + File.separator + "test" + File.separator + taskDay + File.separator + apkId + File.separator + imsi + File.separator + taskType;
        } else {
            fileName = TASK_PATH + File.separator + taskDay + File.separator + apkId + File.separator + imsi + File.separator + taskType;
        }

        return fileName;
    }

    /**
     * 新文件
     *
     * @param imsi
     * @return
     */
    public static String getNewUserFileName(String imsi) {
        return getOssFileNameOfKey(imsi, NEWUSER_PATH);
    }


    private static String getOssFileNameOfKey(String fileName, String path) {
        CacheParamManager cacheParamManager = SpringContextUtils.getBean(CacheParamManager.class);
        Object paramValue = cacheParamManager.getParamValue(CacheKey.TEST);
        String targetFileName;
        if (paramValue != null && "1".equals(paramValue)) {
            targetFileName = "test" + File.separator + path + File.separator + fileName;
        } else {
            targetFileName = path + File.separator + fileName;
        }
        return targetFileName;
    }

    public static String getMobileFileName(String fileName) {
        return getOssFileNameOfKey(fileName, MOBILE_PATH);
    }
}
